package org.web3j.codegen;

import java.math.BigInteger;

import org.bouncycastle.util.encoders.Hex;
import org.web3j.abi.datatypes.Utf8String;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.ECKeyPair;
import org.web3j.crypto.Keys;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.RemoteFunctionCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.gas.DefaultGasProvider;
import org.web3j.tx.gas.StaticGasProvider;
import org.web3j.utils.Convert;
import org.web3j.utils.Numeric;

public class GreeterContract {
	
	private static String privateKey = "00be6b6223868e4ca31bf986fac76e9190fdcfa78bc432647e51cb43599628def7";

	private static String url = "http://192.168.168.5:8545";
	private static String contractAddress = "0x74016b2375211134c8e57a9b2476eab9223b9879";
	
	public static void main(String[] args) throws Exception {
//		ECKeyPair ecKeyPair = Keys.createEcKeyPair();
//		Credentials credentials = Credentials.create(ecKeyPair);
//		BigInteger privKey = new BigInteger(1, ecKeyPair.getPrivateKey().toByteArray());
//		System.out.println(privKey);

//		ECKeyPair ecKeyPair2 = ECKeyPair.create(ecKeyPair.getPrivateKey());
//		System.out.println(ecKeyPair.getPrivateKey());
//		System.out.println(ecKeyPair2.getPrivateKey());
//		System.out.println(ecKeyPair.getPublicKey());
//		System.out.println(ecKeyPair2.getPublicKey());

//		System.out.println(Hex.toHexString(ecKeyPair.getPrivateKey().toByteArray()));

//4cb11b82264fdb19c781ce59fece1618258fd1444a95120530b82c638a0905ba
//34688698278773167497177715647290663385275430743606698944541354898868358677946
		//0x464f490d0bfddf38cb6d98e70295599a6470c13d
//		BigInteger privateKey = //
//				new BigInteger(1, Numeric
//						.hexStringToByteArray("00be6b6223868e4ca31bf986fac76e9190fdcfa78bc432647e51cb43599628def7"));
//		System.out.println(privateKey);
//		System.out.println("34688698278773167497177715647290663385275430743606698944541354898868358677946");

		ECKeyPair ecKeyPair = ECKeyPair.create(new BigInteger(privateKey, 16));
		Credentials credentials = Credentials.create(ecKeyPair);
		
		System.out.println(Hex.toHexString(ecKeyPair.getPublicKey().toByteArray()));

//		deploy(credentials);
		use(credentials);

	}

	private static void deploy(Credentials credentials) {
		Web3j web3j = Web3j.build(new HttpService(url));
//		Credentials credentials = null;//可以根据私钥生成
//		RemoteCall<Greeter> deploy = Greeter.deploy(web3j, credentials,
//				Convert.toWei("10", Convert.Unit.GWEI).toBigInteger(), BigInteger.valueOf(3000000),
//				BigInteger.valueOf(5201314), "my token", "mt");
		//Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, Utf8String _greeting
		BigInteger gasPrice = Convert.toWei("10", Convert.Unit.GWEI).toBigInteger();
		BigInteger gasLimit = BigInteger.valueOf(3000000);
		Utf8String _greeting = new Utf8String("adopapa");
		
		StaticGasProvider gasProvider = new DefaultGasProvider();
		
//		RemoteCall<Greeter> deploy = Greeter.deploy(web3j, credentials, gasPrice, gasLimit, _greeting);
		RemoteCall<Greeter> deploy = Greeter.deploy(web3j, credentials, gasProvider, _greeting);
		
		try {
			Greeter greeter = deploy.send();
			System.out.println(greeter.getContractAddress());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static void use(Credentials credentials) {
		Web3j web3j = Web3j.build(new HttpService(url));
//		String contractAddress = "0x3e6bb1626b0da357ae03c2446c354c7367fbf58b";
		StaticGasProvider gasProvider = new DefaultGasProvider();
		Greeter contract = Greeter.load(contractAddress, web3j, credentials, gasProvider);
		String myAddress = null;
		String toAddress = null;
		BigInteger amount = BigInteger.ONE;
		try {
			
//			TransactionReceipt receipt = contract.newGreeting(new Utf8String("adopapa")).send();
//			System.out.println(receipt.getBlockNumber());
			
			Utf8String greeting = contract.greet().send();
			System.out.println(greeting.getValue());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
